<?php

namespace Apps\AppApi\Method\Activity;

use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Models\Activity;
use Apps\Common\Models\UserBase;
use Apps\Common\Models\ActivityComment;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Libs\PicUrl;


/**
 * 评论活动
 */
class Comments extends BaseMethod
{
    public function run()
    {
        if (!$this->verifyUserAuth()) {
            return false;
        }

        $id = intval($this->getDataItem('id', 0));
        $user_id = intval($this->getDataItem('user_id', 0));
        $content = $this->getDataItem('content', '');

        if ($id == 0 || $user_id == 0) {
            $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, 'URL参数不全');
            return false;
        }
        $contentLenth = $this->utf8_strlen($content);
        if ($contentLenth < 1 || $contentLenth > 100) {
            $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR, '评论字符应在1~100之间.');
            return false;
        }

        $ActivityComment = new ActivityComment();
        $date = date('Y-m-d H:i:s');
        $data = [
            'comment_user_id' => $user_id,
            'comment_content' => $content,
            'addtime' => $date,
            'activity_id' => $id
        ];
        if ($ActivityComment->save($data)) {
            $ActivityComment->getReadConnection()->query("UPDATE activity SET comments = comments + 1 WHERE activity_id = {$id}");

            $userInfo = UserBase::getOne($user_id);
            $this->setResult([
                'comment_id' => $ActivityComment->comment_id,
                'user_id' => $user_id,
                'comment_content' => $content,
                'addtime' => $date,
                'user_gender' => $userInfo['user_gender'],
                'user_cover' => PicUrl::ActivityCover($userInfo['user_cover'], $this->getDi())
            ]);
        } else {
            $this->errorLog(ResultStatus::ERROR, '评论失败.');
        }
    }

}